package ltesim.editors;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.FileNotFoundException;

import ltesim.editors.initPage.LEditorInitPage;
import ltesim.editors.xmlEditor.XMLEditor;
import ltesim.xmlParser.XMLParser;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.widgets.FormToolkit;

/**
 * Custom form based XML editor
 * 
 * @author xirka
 * 
 */
public class LEditor extends FormEditor implements PropertyChangeListener {
	private XMLParser xParser;
	private String fName;
	private boolean bDirty;
	private LEditorPage page0;
	private TextEditor page1;

	/**
	 * Get the parser class for the opened file
	 * 
	 * @return XMLParser
	 */
	public XMLParser getxParser() {
		return xParser;
	}

	/**
	 * Contructior
	 */
	public LEditor() {
		bDirty = false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.forms.editor.FormEditor#addPages()
	 */
	@Override
	protected void addPages() {
		String fAbsPath = ((IFileEditorInput) this.getEditorInput()).getFile()
				.getLocation().toString();
		try {
			xParser = new XMLParser(fAbsPath);
		} catch (FileNotFoundException e) {
			System.err.println("File: " + fAbsPath + " not found");
			e.printStackTrace();
		}

		fName = xParser.getfName();
		this.setPartName(fName);

		// Show the form page according to the xml type
		switch (xParser.getType()) {
		case 1:// Init
			page0 = new LEditorInitPage(this);
			page0.addPropertyChangeListener("dirtyState", this);
			try {
				addPage(page0);
			} catch (PartInitException e) {
				System.err.println("addPage " + xParser.getTypeName()
						+ " failed");
				e.printStackTrace();
			}
			break;
		case 2:// Dlsch
			page0 = new LEditorPage(this);
			page0.addPropertyChangeListener("dirtyState", this);
			try {
				addPage(page0);
			} catch (PartInitException e) {
				System.err.println("addPage " + xParser.getTypeName()
						+ " failed");
				e.printStackTrace();
			}
			break;
		case 3:// Hi
			page0 = new LEditorPage(this);
			page0.addPropertyChangeListener("dirtyState", this);
			try {
				addPage(page0);
			} catch (PartInitException e) {
				System.err.println("addPage " + xParser.getTypeName()
						+ " failed");
				e.printStackTrace();
			}
			break;

		default:
			System.err.println("Invalid file format");
			break;
		}

		try {
			page1 = new XMLEditor();
			int index = addPage(page1, getEditorInput());
			setPageText(index, fName);
		} catch (PartInitException e) {
			System.err.println("addPage rawEditor failed");
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.forms.editor.FormEditor#createToolkit(org.eclipse.swt.
	 * widgets.Display)
	 */
	@Override
	protected FormToolkit createToolkit(Display display) {
		return new FormToolkit(display);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.
	 * IProgressMonitor)
	 */
	@Override
	public void doSave(IProgressMonitor monitor) {
		getEditor(1).doSave(monitor);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.part.EditorPart#doSaveAs()
	 */
	@Override
	public void doSaveAs() {
		// DISABLED
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.forms.editor.FormEditor#pageChange(int)
	 */
	@Override
	protected void pageChange(int newPageIndex) {
		super.pageChange(newPageIndex);
		switch (newPageIndex) {
		case 0:
			page0.refresh();
			break;
		case 1:
			page1.doRevertToSaved();
			break;

		default:
			break;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
	 */
	@Override
	public boolean isSaveAsAllowed() {
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.forms.editor.FormEditor#createPages()
	 */
	@Override
	protected void createPages() {
		super.createPages();
		if (getPageCount() == 1 && getContainer() instanceof CTabFolder) {
			((CTabFolder) getContainer()).setTabHeight(0);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.
	 * PropertyChangeEvent)
	 */
	@Override
	public void propertyChange(PropertyChangeEvent event) {
		if (event.getPropertyName().equals("dirtyState")) {
			bDirty = (boolean) event.getNewValue();
			String editorTitle = (bDirty ? "*" : "") + fName;
			this.setPartName(editorTitle);
		}

	}

}
